//
//  WZProcedureLogCell.m
//  ETong
//
//  Created by wanzhao on 15/12/18.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZProcedureLogCell.h"

@interface WZProcedureLogCell ()

@property (nonatomic, weak) UILabel *arrivalTimeLabel;
@property (nonatomic ,weak) UILabel *endeTimeLabel;
@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UIView *downLine;
@property (nonatomic, weak) UIView *upLine;
@property (nonatomic, weak) UIView *middleLine;
@property (nonatomic, weak) UIView *neiRongView;
@property (nonatomic, weak) UILabel *taskNameLabel;
@property (nonatomic, weak) UILabel *optTypeLabel;
@property (nonatomic , weak) UILabel *assigneeLabel;
@property (nonatomic, weak) UIButton *commentView;

@end

@implementation WZProcedureLogCell

+ (WZProcedureLogCell *)procedureLogCellWithTableView:(UITableView *)tableView
{
    static NSString *procedureLogCellId = @"procedureLogCellId";
    WZProcedureLogCell *cell = [tableView dequeueReusableCellWithIdentifier:procedureLogCellId];
    if (cell == nil) {
        cell = [[WZProcedureLogCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:procedureLogCellId];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = ClearColor;
        self.backgroundColor = ClearColor;
        
        
        UILabel *label = [[UILabel alloc] init];
        label.textColor = RGBCOLORWITH16COLOR(0x707070);
        label.numberOfLines = 0;
        label.font = Font8;
        [self.contentView addSubview:label];
        self.arrivalTimeLabel = label;
        
        UILabel *label1 = [[UILabel alloc] init];
        label1.textColor = RGBCOLORWITH16COLOR(0x707070);
        label1.font = Font8;
        label1.numberOfLines = 0;
        [self.contentView addSubview:label1];
        self.endeTimeLabel = label1;

        UIImageView *imageView = [[UIImageView alloc] init];
        [self.contentView addSubview:imageView];
        self.iconView = imageView;
        
        
        UIView *upLine = [[UIView alloc] init];
        upLine.backgroundColor = RGBCOLORWITH16COLOR(0xe5e5e5);
        [self.contentView addSubview:upLine];
        self.upLine = upLine;
        
        UIView *downLine = [[UIView alloc] init];
        downLine.backgroundColor = RGBCOLORWITH16COLOR(0xe5e5e5);
        [self.contentView addSubview:downLine];
        self.downLine = downLine;
        
        UIView *view = [[UIView alloc] init];
        view.layer.borderWidth = 0.5f;
        view.layer.borderColor = RGBCOLORWITH16COLOR(0xe5e5e5).CGColor;
        view.layer.cornerRadius = 2.0f;
        view.layer.masksToBounds = YES;
        [self.contentView addSubview:view];
        self.neiRongView = view;
        
        
        UILabel *label2 = [[UILabel alloc] init];
        label2.textColor = RGBCOLORWITH16COLOR(0x333333);
        label2.font = Font9;
        label2.numberOfLines = 0;
        [self.neiRongView addSubview:label2];
        self.taskNameLabel = label2;

        UILabel *label3 = [[UILabel alloc] init];
        label3.textColor = RGBCOLORWITH16COLOR(0x333333);
        label3.font = Font14;
        label3.numberOfLines = 0;
        [self.neiRongView addSubview:label3];
        self.assigneeLabel = label3;

        UILabel *label4 = [[UILabel alloc] init];
        label4.textColor = RGBCOLORWITH16COLOR(0xf13030);
        label4.font = Font9;
        label4.textAlignment = UITextAlignmentRight;
        label4.numberOfLines = 0;
        [self.neiRongView addSubview:label4];
        self.optTypeLabel = label4;

        UIView *middleLine = [[UIView alloc] init];
        middleLine.backgroundColor = RGBCOLORWITH16COLOR(0xe5e5e5);
        [self.neiRongView addSubview:middleLine];
        self.middleLine = middleLine;

        
        UIButton *label5 = [UIButton buttonWithType:UIButtonTypeCustom];
        [label5 setTitleColor:RGBCOLORWITH16COLOR(0x333333) forState:UIControlStateNormal];
        label5.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
        label5.titleLabel.numberOfLines = 3;
        label5.titleLabel.font = Font9;
        label5.userInteractionEnabled = NO;
        label5.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
        label5.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.neiRongView addSubview:label5];
        self.commentView = label5;
        
    }
    return self;
}


- (void)setProcedureLog:(WZProcedureLog *)procedureLog
{
    _procedureLog = procedureLog;
    if (procedureLog.isShortCell) {
        self.middleLine.hidden = YES;
        self.commentView.hidden = YES;
        self.upLine.hidden = YES;
        self.iconView.image = [UIImage imageNamed:@"smallLiuchengRizhi"];
    } else {
        self.middleLine.hidden = NO;
        self.commentView.hidden = NO;
        self.iconView.image = [UIImage imageNamed:@"liuchengrizhi"];
        self.upLine.hidden = NO;
    }
    self.arrivalTimeLabel.text = procedureLog.arrivalTime;
    self.endeTimeLabel.text = procedureLog.endTime;
    self.taskNameLabel.text = procedureLog.taskName;
    self.assigneeLabel.text = procedureLog.assignee;
    self.optTypeLabel.text = procedureLog.optType;
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"执行意见：%@",procedureLog.comment]];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
//    paragraphStyle.lineBreakMode = UILineBreakModeTailTruncation;
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    paragraphStyle.maximumLineHeight = 60;  //最大的行高
    paragraphStyle.lineSpacing = 3.0f;  //行自定义行高度
    
//    [paragraphStyle setFirstLineHeadIndent:self.usernameLabel.frame.size.width + 5];//首行缩进 根据用户昵称宽
    
    [att addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle,
                         NSForegroundColorAttributeName:RGBCOLORWITH16COLOR(0x333333)
                         } range:NSMakeRange(0, att.string.length)];
    [self.commentView setAttributedTitle:att forState:UIControlStateNormal];
    
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.arrivalTimeLabel.frame = CGRectMake(OppositeWidthRate(15), (self.height - Font8.lineHeight * 4 - OppositeHeightRate(10)) / 2.0, OppositeWidthRate(45), Font8.lineHeight * 2);
    self.endeTimeLabel.frame
    = CGRectMake(OppositeWidthRate(15), CGRectGetMaxY(self.arrivalTimeLabel.frame) + OppositeHeightRate(10), OppositeWidthRate(45), Font8.lineHeight * 2);
    
    self.iconView.frame = CGRectMake(CGRectGetMaxX(self.arrivalTimeLabel.frame), (self.height - self.iconView.image.size.height) / 2.0f, self.iconView.image.size.width, self.iconView.image.size.height);
    self.iconView.centerX = CGRectGetMaxX(self.arrivalTimeLabel.frame) + OppositeWidthRate(12);
    
    self.upLine.frame = CGRectMake(0, 0, 0.5f, (self.height - self.iconView.image.size.height) / 2.0f);
    self.upLine.centerX = self.iconView.centerX;
    
    self.downLine.frame = CGRectMake(0, CGRectGetMaxY(self.iconView.frame), 0.5f, (self.height - self.iconView.image.size.height) / 2.0f);
    self.downLine.centerX = self.iconView.centerX;
    
    self.neiRongView.frame = CGRectMake(CGRectGetMaxX(self.arrivalTimeLabel.frame) + OppositeWidthRate(32), OppositeHeightRate(10), self.width - CGRectGetMaxX(self.arrivalTimeLabel.frame) - OppositeWidthRate(32) - OppositeWidthRate(15), self.height - OppositeHeightRate(10));
    
    self.taskNameLabel.frame = CGRectMake(OppositeWidthRate(10), OppositeHeightRate(5), self.neiRongView.width / 2.0f - OppositeWidthRate(10) ,Font9.lineHeight);
    self.optTypeLabel.frame = CGRectMake(self.neiRongView.width / 2.0, OppositeHeightRate(5), self.neiRongView.width / 2.0f - OppositeWidthRate(10), Font9.lineHeight);
    self.assigneeLabel.frame = CGRectMake(OppositeWidthRate(10), CGRectGetMaxY(self.taskNameLabel.frame) + OppositeHeightRate(5), self.width - OppositeWidthRate(20), Font14.lineHeight);
    
    self.middleLine.frame = CGRectMake(OppositeWidthRate(10), CGRectGetMaxY(self.assigneeLabel.frame), self.neiRongView.width - OppositeWidthRate(20), 0.5f);
    self.commentView.frame = CGRectMake(OppositeWidthRate(10), CGRectGetMaxY(self.middleLine.frame) + 3.0f, self.middleLine.width, self.neiRongView.height - CGRectGetMaxY(self.middleLine.frame));
}

@end
